﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;

namespace Waaahsabi.Controls.TheGame
{
	public class GenericTargetingControl : Control
	{
		#region Label DependencyProperty
		public static readonly DependencyProperty LabelProperty =
		DependencyProperty.Register
			(
			"Label",
			typeof(string),
			typeof(GenericTargetingControl),
			new PropertyMetadata("NoName")
			);

		public string Label
		{
			get { return (string)GetValue(LabelProperty); }
			set { SetValue(LabelProperty, value); }
		}
		#endregion
		#region StatusText DependencyProperty
		public static readonly DependencyProperty StatusTextProperty =
		DependencyProperty.Register
			(
			"StatusText",
			typeof(string),
			typeof(GenericTargetingControl),
			new PropertyMetadata(null)
			);

		public string StatusText
		{
			get { return (string)GetValue(StatusTextProperty); }
			set { SetValue(StatusTextProperty, value); }
		}
		#endregion

		private bool _isDragging;
		private Storyboard _progressAnimation;
		private Storyboard _reverseProgressAnimation;
		private Button _btnQuitButton;

		public event EventHandler<MouseEventArgs> ControlMoving;
		public new event EventHandler<MouseButtonEventArgs> Drop;
		public event RoutedEventHandler QuitButtonClick;
		public event EventHandler AnimationCompleted;
		private Point _dragStartPoint;
		private readonly bool _useBypasserStyle;

		public GenericTargetingControl(bool useBypasserTemplate = false)
		{
			DefaultStyleKey = typeof (GenericTargetingControl);
			_useBypasserStyle = useBypasserTemplate;
		}

		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			_progressAnimation = GetTemplateChild("ProgressAnimation") as Storyboard;
			_reverseProgressAnimation = GetTemplateChild("ReverseProgressAnimation") as Storyboard;
			if (_progressAnimation != null)
				_progressAnimation.Completed += delegate { if (AnimationCompleted != null) AnimationCompleted(this, null); };
			if (_reverseProgressAnimation != null)
				_reverseProgressAnimation.Completed += delegate { if (AnimationCompleted != null) AnimationCompleted(this, null); };

			_btnQuitButton = GetTemplateChild("btnQuitButton") as Button;
			if(_btnQuitButton != null)
			{
				_btnQuitButton.Click += QuitButtonClick;
			}
			if (_useBypasserStyle)
				VisualStateManager.GoToState(this, "Bypasser", false);
		}

		protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
		{
			base.OnMouseLeftButtonDown(e);
			_dragStartPoint = e.GetPosition(null);
			_isDragging = true;
			CaptureMouse();
		}

		protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
		{
			base.OnMouseLeftButtonUp(e);
			_isDragging = false;
			ReleaseMouseCapture();

			if(_dragStartPoint == e.GetPosition(null))
				return;
			if (Drop != null)
				Drop(this, e);
		}

		protected override void OnMouseMove(MouseEventArgs e)
		{
			base.OnMouseMove(e);
			if(_isDragging)
			{
				if (ControlMoving != null)
					ControlMoving(this, e);
				Point currentPosition = e.GetPosition(null);
				Canvas.SetLeft(this, currentPosition.X - 2);
				Canvas.SetTop(this, currentPosition.Y - RenderSize.Height + 2);
			}
		}
		/// <summary>
		/// Plays back the progress bar animation. Default animation length is 1000 ms.
		/// </summary>
		/// <param name="speedMultiplier">Ratio of speed for playback. 0.5 = double the time, 2 = half the time.</param>
		public void ShowProgressAnimation(double speedMultiplier)
		{
			_progressAnimation.SpeedRatio = speedMultiplier;
			_progressAnimation.Begin();
		}

		/// <summary>
		/// Plays back the progress bar animation, but BACKWARDS. Default animation length is 1000 ms.
		/// </summary>
		/// <param name="speedMultiplier">Ratio of speed for playback. 0.5 = double the time, 2 = half the time.</param>
		public void ShowReverseProgressAnimation(double speedMultiplier)
		{
			_reverseProgressAnimation.SpeedRatio = speedMultiplier;
			_reverseProgressAnimation.Begin();
		}

		public void InterruptAnimation()
		{
			_progressAnimation.Stop();
			_reverseProgressAnimation.Stop();
		}

		public void ClearAnimationEventHandlers()
		{
			AnimationCompleted = null;
		}
	}
}
